{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myCRC配置:\n",
      "width       = 8\n",
      "poly        = 0x31\n",
      "reflect_in  = True\n",
      "xor_in      = 0x0\n",
      "reflect_out = True\n",
      "xor_out     = 0x0\n",
      "-------------------------------------------------------------\n",
      "输入数据: 0x36261E4\n",
      "myCrc最终CRC值二进制: 01110100\n",
      "myCrc最终CRC值16进制: 0x74\n",
      "-------------------------------------------------------------\n",
      "CRC配置一致！\n",
      "pyCRC的CRC结果是: 0x74\n",
      "myCRC和pyCRC的结果一致！\n"
     ]
    }
   ],
   "source": [
    "import pycrc.algorithms\n",
    "import myCRC\n",
    "import random\n",
    "import math\n",
    "\n",
    "# myCRC示例用法\n",
    "crc_custom = myCRC.CRCConfig(\n",
    "    width       = 8    ,\n",
    "    poly        = 0x31,\n",
    "    reflect_in  = True  ,\n",
    "    xor_in      = 0x00,\n",
    "    reflect_out = True  ,\n",
    "    xor_out     = 0x00\n",
    ")\n",
    "\n",
    "# 打印CRC配置\n",
    "print(\"myCRC配置:\")\n",
    "print(f\"{'width':<12}= \" + str(crc_custom.width))\n",
    "print(f\"{'poly':<12}= \" + f\"0x{crc_custom.poly:X}\")\n",
    "print(f\"{'reflect_in':<12}= \" + str(crc_custom.reflect_in))\n",
    "print(f\"{'xor_in':<12}= \" + f\"0x{crc_custom.xor_in:X}\")\n",
    "print(f\"{'reflect_out':<12}= \" + str(crc_custom.reflect_out))\n",
    "print(f\"{'xor_out':<12}= \" + f\"0x{crc_custom.xor_out:X}\")\n",
    "print('-------------------------------------------------------------')\n",
    "\n",
    "\n",
    "# 随机选择位宽\n",
    "bitwidth = random.randint(1, 64)\n",
    "max_value_for_bitwidth = (1 << bitwidth) - 1\n",
    "input_data = random.randint(0, max_value_for_bitwidth) # 随机输入\n",
    "# input_data = 0x00004c8dc8812d # 手动输入数据\n",
    "\n",
    "print(f\"输入数据: 0x{input_data:X}\")\n",
    "mycrc_result = crc_custom.calc_crc(input_data)\n",
    "final_crc_hex = hex(int(mycrc_result, 2))[2:].upper().zfill(math.ceil(len(mycrc_result) / 4))\n",
    "print(f\"myCrc最终CRC值二进制: {mycrc_result}\")\n",
    "print(f\"myCrc最终CRC值16进制: 0x{final_crc_hex}\")\n",
    "print('-------------------------------------------------------------')\n",
    "\n",
    "# pycrc示例用法\n",
    "crc = pycrc.algorithms.Crc(\n",
    "    width       = 8    ,\n",
    "    poly        = 0x31,\n",
    "    reflect_in  = True  ,\n",
    "    xor_in      = 0x00,\n",
    "    reflect_out = True  ,\n",
    "    xor_out     = 0x00\n",
    ")\n",
    "# 检查CRC配置是否一致\n",
    "assert crc.width == crc_custom.width\n",
    "assert crc.poly == crc_custom.poly\n",
    "assert crc.reflect_in == crc_custom.reflect_in\n",
    "assert crc.xor_in == crc_custom.xor_in\n",
    "assert crc.reflect_out == crc_custom.reflect_out\n",
    "assert crc.xor_out == crc_custom.xor_out\n",
    "print(\"CRC配置一致！\")\n",
    "\n",
    "# 将整数转换为字节序列，假设是大端字节序\n",
    "input_data_list = input_data.to_bytes((input_data.bit_length() + 7) // 8, byteorder='big') or bytes([0])\n",
    "pycrc_result = crc.bit_by_bit(input_data_list)\n",
    "the_pycrc_result = hex(pycrc_result)[2:].upper().zfill(math.ceil(len(mycrc_result) / 4))\n",
    "print(f\"pyCRC的CRC结果是: 0x{the_pycrc_result}\")\n",
    "\n",
    "# 判断两个16进制是否一样\n",
    "if final_crc_hex == the_pycrc_result:\n",
    "    print(\"myCRC和pyCRC的结果一致！\")\n",
    "else:\n",
    "    print(\"myCRC和pyCRC的结果不一致！\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
